//
//  SelectViewController.m
//  yuyinDemo
//
//  Created by 王帅强 on 2017/12/4.
//  Copyright © 2017年 小强~~. All rights reserved.
//

#import "SelectViewController.h"
#import "SelectLanguageCell.h"
#import "LanguageModel.h"
#import "TranslationManager.h"
#import "Const_OC.h"
@interface SelectViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) NSMutableArray * recentLanguages;

@end

@implementation SelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupOCVC];
    [self initData];
}

- (NSMutableArray *)recentLanguages {
    
    if (!_recentLanguages) {
        
        if ([TranslationManager sharedManager].recentLanguages) {
            
            _recentLanguages = [NSMutableArray arrayWithArray:[TranslationManager sharedManager].recentLanguages];
        }else{
            
            _recentLanguages = [[NSMutableArray alloc] init];
        }
    }
    
    return _recentLanguages;
}
- (void)initData {

    self.title = LocalizedString(@"choose_language");
    self.view.backgroundColor = kThemeBgColor;
    self.tableView.backgroundColor = kThemeBgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"SelectLanguageCell" bundle:nil] forCellReuseIdentifier:@"SelectLanguageCell"];
    

    NSArray *arr = @[@{@"name_local":@"trans_1", @"code":@"zh-CN", @"code1":@"zh"},
                        @{@"name_local":@"trans_2", @"code":@"zh-HK", @"code1":@"yue"},
                        @{@"name_local":@"trans_3", @"code":@"en-AU", @"code1":@"en"},
                        @{@"name_local":@"trans_4", @"code":@"fr-CA", @"code1":@"fra"},
                        @{@"name_local":@"trans_5", @"code":@"de-DE", @"code1":@"de"},
                        @{@"name_local":@"trans_6", @"code":@"ja-JP", @"code1":@"jp"},
                        @{@"name_local":@"trans_7", @"code":@"ko-KR", @"code1":@"kor"},
                        @{@"name_local":@"trans_8", @"code":@"ru-RU", @"code1":@"ru"},
                        @{@"name_local":@"trans_9", @"code":@"ar-SA", @"code1":@"ara"},
                        @{@"name_local":@"trans_10", @"code":@"cs-CZ", @"code1":@"cs"},
                        @{@"name_local":@"trans_11", @"code":@"da-DK", @"code1":@"dan"},
                        @{@"name_local":@"trans_12", @"code":@"nl-BE", @"code1":@"nl"},
                        @{@"name_local":@"trans_13", @"code":@"fi-FI", @"code1":@"fin"},
                        @{@"name_local":@"trans_14", @"code":@"el-GR", @"code1":@"el"},
                        @{@"name_local":@"trans_15", @"code":@"hu-HU", @"code1":@"hu"},
                        @{@"name_local":@"trans_16", @"code":@"it-IT", @"code1":@"it"},
                        @{@"name_local":@"trans_17", @"code":@"pl-PL", @"code1":@"pl"},
                        @{@"name_local":@"trans_18", @"code":@"pt-BR", @"code1":@"pt"},
                        @{@"name_local":@"trans_19", @"code":@"ro-RO", @"code1":@"rom"},
                        @{@"name_local":@"trans_20", @"code":@"es-ES", @"code1":@"spa"},
                        @{@"name_local":@"trans_21", @"code":@"sv-SE", @"code1":@"swe"},
                        @{@"name_local":@"trans_22", @"code":@"th-TH", @"code1":@"th"}];
    [self loadDataWith:arr];
    [self.tableView reloadData];
}


-(void)loadDataWith:(NSArray *)arr{
    self.dataSource = [NSMutableArray new];
    for (NSDictionary *dict in arr) {
        LanguageModel * model = [LanguageModel new];
        
        model.lang = dict[@"code"];
        model.translation = dict[@"code1"];
        model.name_local = dict[@"name_local"];
        [self.dataSource addObject:model];
    }
    
    if (self.recentLanguages.count > 0) {
        [self DuplicateRemoval];
    }
}
#pragma - mark delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.recentLanguages.count > 0) {
        return 2;
    }
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0 && self.recentLanguages.count > 0) {
        
        return self.recentLanguages.count;
    }
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectLanguageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectLanguageCell"];
    LanguageModel  *lang;
    if (indexPath.section == 0 && self.recentLanguages.count > 0){
        lang = self.recentLanguages[indexPath.row];
    }else{
       lang = self.dataSource[indexPath.row];
    }
    
    cell.languageNameLabel.text = LocalizedString(lang.name_local);
    if ([self.currentLanguage isEqualToString:lang.lang]) {
        cell.isSelect =YES;
    }else{
        cell.isSelect =NO;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 56;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LanguageModel *lang;
    if (indexPath.section == 0 && self.recentLanguages.count > 0) {
        lang = self.recentLanguages[indexPath.row];
    }else{
        lang = self.dataSource[indexPath.row];
    }
    
    if ([lang.lang isEqualToString:self.currentLanguage]) {
        
        return;
    }
    self.currentLanguage = lang.lang;
    if (self.block) {
        self.block(lang);
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    [self collectRecentlyLanguage:lang];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)collectRecentlyLanguage:(LanguageModel *)model {
    
    [self deletModel:model];
    [self.recentLanguages insertObject:model atIndex:0];
    
    if (self.recentLanguages.count > 5) {
        
        [self.recentLanguages removeObjectAtIndex:5];
    }
    [TranslationManager sharedManager].recentLanguages = self.recentLanguages;
}

- (void)deletModel:(LanguageModel*)mode {
    for (int i = 0; i < self.recentLanguages.count; i++) {
        
        LanguageModel * lang = self.recentLanguages[i];
        if ([lang.lang isEqualToString:mode.lang]) {
            [self.recentLanguages removeObject:lang];
            break;
        }
    }
}


- (void)DuplicateRemoval {
    
    NSMutableArray * arr = [NSMutableArray new];
    LanguageModel * mm;
    for (LanguageModel * model in self.recentLanguages) {
        
        for (LanguageModel *lang in self.dataSource) {
            if ([lang.lang isEqualToString:model.lang]) {
                [arr addObject:lang];
            }else if ([lang.lang isEqualToString:self.seletedLanguagel]) {
                [arr addObject:lang];
            }
        }
        if ([model.lang isEqualToString:self.seletedLanguagel]) {
            mm = model;
        }
    }
        
    if (mm) {
            [self.recentLanguages removeObject:mm];
    }
        
    [self.dataSource removeObjectsInArray:arr];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, kScreenWidth - 32, 30)];
    label.textColor = kSubitleColor;
    label.font = [UIFont systemFontOfSize:14];
    
    UIView *view = [UIView new];
    
    [view addSubview:label];
    
    if (section == 0 && self.recentLanguages.count > 0) {
        
        label.text = LocalizedString(@"recently_language");
        
        return view;
    }
    
    label.text = LocalizedString(@"all_language");
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (IBAction)sureClick:(UIButton *)sender {

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
